import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
// 导入组件文件
import "./element";
import "font-awesome/css/font-awesome.min.css";
// 导入axios vue-axios
// 引入axios
import axios from "axios";
import VueAxios from "vue-axios";
Vue.use(VueAxios, axios);

// 引入echarts图表框架
import echarts from "echarts";
Vue.prototype.$echarts = echarts;

// 引入mqtt组件
import mqtt from "mqtt";
Vue.prototype.$mqtt = mqtt;

// 配置过滤器转换时间
Vue.filter("mmsgFormat", function(time) {
  let date = new Date(time);
  // 年
  let year = date.getFullYear();
  // 月
  let month =
    date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1;
  // 日
  let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  // 时
  let h = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  // 分
  let m = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  // 秒
  let s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

  let currentTime = `${year}-${month}-${day} ${h}:${m}:${s}`;
  return currentTime;
});
// 配置请求基础路径
axios.defaults.baseURL = "http://kasbackend.com";
//配置post请求头
// axios.defaults.headers.post["Content-Type"] =
//   "application/x-www-form-urlencodedcharset=utf-8";
// // 发起请求之前处理
axios.interceptors.request.use((config) => {
  // console.log(config);
  if (config.method == "post") {
    let paramsString = "";
    //post请求参数序列化，转成一个字符串
    for (let key in config.data) {
      paramsString += key + "=" + config.data[key] + "&";
    }
    config.data = paramsString.slice(0, -1);
  }
  // 把令牌添加到Authorization里面认证
  config.headers.Authorization = localStorage.getItem("token");
  return config;
});

// // 路由拦截
// router.beforeEach((to, from, next) => {
//   console.log( "全局守卫==》",localStorage.getItem("token"));

//   if (localStorage.getItem("token") == "") {
//     // console.log("没有令牌你不能进去");
//     next({path: '/'})
//     // console.log(next);
//   } else {
//     // console.log("你有令牌可以进去");
//     next();
//   }
// });

// 引入路由组件
import VueRouter from "vue-router";

Vue.config.productionTip = false;

// 设置重复点击的错误
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");
